<?php

namespace App\Http\Controllers\Common;

use App\Helpers\Code;
use App\Models\Common\MenuAction;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class MenuActionController extends Controller
{

    public function __construct()
    {
        $this->model = new MenuAction();

        $this->_validations();
    }

    /**
     * 添加菜单功能
     * @param Request $request
     * @return array
     */
    public function addAction(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }
            $model = $this->model->addAction($data);
            $this->checked($model, [Code::INSIDE_ERROR => 'menu.menu_title_unique']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 删除菜单功能
     * @param $id
     * @return array
     * todo:后期需要完善   目前只是简单的删除   没有涉及关联判断
     */
    public function deleteAction($id)
    {
        try {
            $model = $this->model->deleteAction($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'menu.action.menu_action_exist_permission'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 修改菜单功能
     * @param $id
     * @param Request $request
     * @return array
     */
    public function editAction($id, Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));

            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'menu_id':
                        break;
                    case 'code':
                        $rules['code'] = array('bail', 'required', 'max:100', Rule::unique('menu_actions')->ignore($id));
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                        break;
                }
            }

            if (isset($data['menu_id'])) {
                unset($data['menu_id']);
            }

            $validator = Validator::make($data, $rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editAction($id, $data);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'menu.menu_title_unique'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 为指定的功能添加对应的接口
     * @param $id
     * @param Request $request
     * @return array
     */
    public function editActionPermission($id, Request $request)
    {
        try {
            if (!$request->has('permission')) {
                return $this->error('menu.permission_necessarily');
            }

            $permission = $request->input('permission');

            if (!is_array($permission)) {
                return $this->error('common.data_format_error');
            }

            $model = $this->model->editActionPermission($id, $permission);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
                Code::INSIDE_ERROR => 'menu.permission_not_exits'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }


    /**
     * 获取指定的功能对应的接口
     * @param $id
     * @return array
     */
    public function getActionPermissionAll($id)
    {
        try {
            $model = $this->model->getActionPermissionAll($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validations()
    {
        $this->rules = [
            'menu_id' => 'bail|required|numeric|exists:menus,id',
            'title' => 'bail|required|max:100',
            'code' => 'bail|required|max:100|unique:menu_actions',
            'description' => 'max:200',
        ];

        $this->messages = [
            'menu_id.required' => trans('menu.menu_id_required'),
            'menu_id.numeric' => trans('menu.menu_id_numeric'),
            'menu_id.exists' => trans('menu.menu_id_exists'),
            'title.required' => trans('menu.title_required'),
            'title.max' => trans('menu.title_max'),
            'code.required' => trans('menu.code_required'),
            'code.max' => trans('menu.code_max'),
            'code.unique' => trans('menu.code_unique'),
            'description.max' => trans('menu.description_max'),
        ];
    }
}
